﻿using System;
using System.Collections.Generic;
using System.Linq;
using Styx;
using Styx.CommonBot;
using Styx.CommonBot.POI;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;

namespace PPHelper
{
    class Helpers
    {
        private static readonly LocalPlayer Me = StyxWoW.Me;

        public static bool ViableForCast(string spellName)
        {
            return (AuthorizedSprintZonesIds.Contains(Me.MapId) && SpellManager.CanCast(spellName) && IsViable(Me) && !Me.Mounted && !Me.IsOnTransport && !Me.IsResting && !Me.IsDead && !Me.IsGhost && !Me.IsSwimming && Me.IsMoving && !Me.HasAura(SpellsAuraDic[spellName]));
        }

        #region Cast Handlers

        public static void ClearTargetAfterVanish(object sender, LuaEventArgs args)
        {
            /* LOGGING PURPOSE
            
            int argCount = 0;
            foreach (var data in args.Args)
            {
                Main.Log(string.Format("arg[{0}] : {1}", argCount, data));
                argCount++;
            }
             
            */

            if (Convert.ToUInt32(args.Args[4]) != 1856) return;

            Main.Log("Vanish Cast has been detected, clearing targets and resetting BotPOI !");
            BotPoi.Current.Type = PoiType.None;
            Targeting.Instance.TargetList.Clear();
            Me.ClearTarget();
        }

        #endregion

        #region Misc Helpers

        public static TimeSpan GetSpellCooldown(string spell)
        {
            SpellFindResults results;
            if (SpellManager.FindSpell(spell, out results))
                return results.Override != null ? results.Override.CooldownTimeLeft : results.Original.CooldownTimeLeft;

            return TimeSpan.MaxValue;
        }

        public static bool IsViable(WoWObject wowObject)
        {
            return (wowObject != null) && wowObject.IsValid && StyxWoW.IsInGame && StyxWoW.IsInWorld;
        }

        public static bool IsTravelling()
        {
            return
                ObjectManager.GetObjectsOfType<WoWGameObject>(false, false).Any(
                    ship => ship != null && ship.IsValid && ship.Transport && ship.Distance < 50);
        }

        public static bool CanUseHearthStoneSafely
        {
            get
            {
                return ObjectManager.GetObjectsOfType<WoWUnit>().Any(unit => unit.IsHostile && unit.Distance < (double)unit.GetAggroRange(Me));
            }
        }

        public static Dictionary<string, int> SpellsAuraDic = new Dictionary<string, int>
                                                                  {
                                                                        {"Sprint", 2983},
                                                                  };

        public static List<uint> AuthorizedSprintZonesIds = new List<uint>
                                                                {
                                                                    870,
                                                                };

        #endregion
    }
}
